<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客房编号" prop="numberId">
        <el-input
          v-model="queryParams.numberId"
          placeholder="请输入客房编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
   
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:config:add']"
        >新增</el-button>
        
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="55" align="center" prop="id" >
        <template slot-scope="scope">
          <!-- 计算序号 -->
          {{ (queryParams.pageNum - 1) * queryParams.pageSize+ scope.$index + 1  }}
        </template>
      </el-table-column>
      <el-table-column label="客房编号" align="center" prop="numberId" :show-overflow-tooltip="true" />
      <el-table-column label="客房类型" align="center" prop="roomType" :show-overflow-tooltip="true" />
      <el-table-column label="客房价格" align="center" prop="roomPrice" :show-overflow-tooltip="true" />
      <el-table-column label="客房楼层" align="center" prop="roomFloor" :show-overflow-tooltip="true" />
      <el-table-column label="房间朝向" align="center" prop="roomToward" :show-overflow-tooltip="true" />
      <el-table-column label="客房状态" align="center" prop="houseNull" :show-overflow-tooltip="true" />
      <el-table-column label="客房描述" align="center" prop="houseDesc" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:config:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="delGuestRoom(scope.row)"
            v-hasPermi="['system:config:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客房编号" prop="numberId">
          <el-input v-model="form.numberId" placeholder="请输入客房编号" />
        </el-form-item>
        <el-form-item label="客房类型" prop="roomType">
          <el-input v-model="form.roomType" placeholder="请输入客房类型" />
        </el-form-item>
        <el-form-item label="客房价格" prop="roomPrice">
          <el-input v-model="form.roomPrice" placeholder="请输入客房价格" />
        </el-form-item>
        <el-form-item label="客房楼层" prop="roomFloor">
          <el-input v-model="form.roomFloor" placeholder="请输入客房楼层" />
        </el-form-item>
        <el-form-item label="房间朝向" prop="roomToward">
          <el-input v-model="form.roomToward" placeholder="请输入房间朝向" />
        </el-form-item>
        <el-form-item label="客房状态" prop="houseNull">
          <el-select v-model="form.houseNull" placeholder="请选择客房状态" style="width: 340px">
            <el-option
              v-for="item in houseNullOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="客房描述" prop="houseDesc">
          <el-input v-model="form.houseDesc" type="textarea" placeholder="请输入客房描述" />
        </el-form-item>
       

      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button type="primary" @click="addGuestRoom">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGuestRoom, getGuestRoomKey, addGuestRoom, updateGuestRoom, delGuestRoom, refreshCache } from "@/api/system/guestRoom";

export default {
  name: "Config",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        numberId: undefined,
        configName: undefined,
        configKey: undefined,
        houseNull: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        numberId: [
          { required: true, message: "客房编号不能为空", trigger: "blur" }
        ],
        roomType: [
          { required: true, message: "客房类型不能为空", trigger: "blur" }
        ],
        roomPrice: [
          { required: true, message: "客房价格不能为空", trigger: "blur" }
        ],
        roomFloor: [
          { required: true, message: "客房楼层不能为空", trigger: "blur" }
        ],
        roomToward: [
          { required: true, message: "房间朝向不能为空", trigger: "blur" }
        ],
        houseDesc: [
          { required: true, message: "客房描述不能为空", trigger: "blur" }
        ]
      },
      houseNullOptions: [
        { value: '已入住', label: '已入住' },
        { value: '清洁中', label: '清洁中' },
        { value: '空闲', label: '空闲' }
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listGuestRoom(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.configList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        numberId: undefined,
        roomType: undefined,
        roomPrice: undefined,
        roomFloor: undefined,
        roomToward: undefined,
        houseNull: "空闲",
        houseDesc: "干净整洁，舒适大方。"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客房";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const numberId = row.numberId || this.ids
      getGuestRoomKey(numberId).then(response => {
        this.form = response.rows[0];
        this.open = true;
        this.title = "修改客房";
      });
    },
    /** 提交按钮 */
    // submitForm: function() {
      addGuestRoom: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.roomPrice=Number( this.form.roomPrice);
          if (this.form.id != undefined) {
            updateGuestRoom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGuestRoom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    delGuestRoom(row) {
      const roomType = row.roomType || this.ids;
      const configIds = row.numberId || this.ids;
      this.$modal.confirm('确认删除 ' + roomType + ' 的房间吗？').then(function() {
          return delGuestRoom(configIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/config/export', {
        ...this.queryParams
      }, `config_${new Date().getTime()}.xlsx`)
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
      });
    }
  }
};
</script>
